<?php
namespace App\Service\Admin;

class WeixiuService extends BaseService
{
    //状态
    protected static $statuss = [
        1 => '新发布', '进行中', '已完成',
    ];

    //列表
    public static function index($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwXiuli)->where('del', 0);
        if ($pas['shop_id']) {
            $db = $db->where('shop_id', $pas['shop_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $statuss = self::$statuss;
        $arys = [];
        foreach ($res as $v) {
            $proOne = self::dbConn(self::$dbYwProject)->where('id', $v->pro_id)->first();
            $pro_name = empty($proOne) ? '-' : $proOne->title;
            $detail = mb_strlen($v->detail)>100 ? mb_substr($v->detail, 0 , 99).'...' : $v->detail;
            $status_name = array_key_exists($v->status, $statuss) ? $statuss[$v->status] : '-';
            $lirun = toNumber($v->money - $v->cost);
            $gotime = $v->gotime ? $v->gotime : '-';
            $oktime = $v->oktime ? $v->oktime : '-';
            $shopOne = self::dbConn(self::$dbYwShop)
                ->where('id', $v->shop_id)
                ->first();
            $shop_name = empty($shopOne) ? '' : $shopOne->title;
            $arys[] = [
                'id' => $v->id, 'orderno' => $v->orderno,
                'cnt' => $v->detail, 'detail' => $detail,
                'pro_id' => $v->pro_id, 'pro_name' => $pro_name,
                'cost' => toNumber($v->cost), 'money' => toNumber($v->money),
                'lirun' => $lirun,
                'status' => $v->status, 'status_name' => $status_name,
                'shop_id' => $v->shop_id, 'shop_name' => $shop_name,
                'addtime' => $v->uptime, 'gotime' => $gotime, 'oktime' => $oktime,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //新增
    public static function add($pas)
    {
        $pas['orderno'] = toCode();
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('del', 0)
            ->where('detail', $pas['detail'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在雷同数据');
        }
        $res = self::dbConn(self::$dbYwXiuli)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //新增
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwXiuli)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //开工
    public static function go($id)
    {
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        if ($res->status > 1) {
            return self::toback(-2, '状态错误');
        }
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('id', $id)
            ->update(['status' => 2, 'gotime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //完成
    public static function finish($id)
    {
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        if ($res->status > 2) {
            return self::toback(-2, '状态错误');
        }
        $res = self::dbConn(self::$dbYwXiuli)
            ->where('id', $id)
            ->update(['status' => 3, 'oktime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}